package org.zfes.snowier.cloud.zbss.metting.router.service.pub;


import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zfes.snowier.auth.shiro.suser.SessionUser;
import org.zfes.snowier.auth.shiro.util.SecurityUtil;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.acc.RegistDto;
import org.zfes.snowier.cloud.zbss.metting.biz.model.TenantMember;
import org.zfes.snowier.cloud.zbss.metting.biz.service.IMemberService;
import org.zfes.snowier.web.controller.BaseController;


@Controller("org.zfes.snowier.cloud.zbss.metting.router.service.pub.MemberController") 
@RequestMapping("/met/service/pub/userInfo")
public class MemberController  extends BaseController {

		@Autowired IMemberService memberService;
	
	    @RequestMapping(value="/userInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public @ResponseBody  Object loadMettingsDto(Long id){
	    	if(!SecurityUtil.isAuthenticated()) {
	    		return ajaxDoneError("user is not login");
	    	}
	    	SessionUser su=
	    			//new SessionUser().setUsername("admin").setLogo("http://image.biaobaiju.com/uploads/20180918/15/1537256494-ZnSKMzEoBI.jpeg");//
	    				SecurityUtil.getSessionUser().get();
	    	
			return ajaxQuery("userInfo",su);
	    }
	    
	    
	 
	    @RequestMapping(value = "/registSendSmsCheckCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public @ResponseBody Object registSendSmsCheckCode(HttpServletRequest request, String phone) {
	    	//memberService.step1SendSmsCheckCode(phone,IPUtil.getClientIpAddr(request));
	        return ajaxDoneSuccess("短信发送成功");
	    }

	   
	    @RequestMapping(value = "/registCheckSmsCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public @ResponseBody Object checkSmsCode(HttpServletRequest request, String phone, String checkCode) {
	    	//Long smsVerfyId = memberService.step2CheckSmsCode(phone, checkCode, IPUtil.getClientIpAddr(request));
	    	return ajaxDoneSuccess("短信验证成功，请进行下一步").put("smsVerfyId", 100);
    //		        return ajaxServerError("验证码错误");
	    }

	    @RequestMapping(value = "/registDoCreateAccount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public @ResponseBody Object createAccount(RegistDto registDto) {
	        Long userId = memberService.step3CreateAccount(registDto);
	        return ajaxDoneSuccess("账号创建成功，请进行下一步").put("userId", userId);
	    }
//============================================================================
	    @RequestMapping(value = "/chengePwdSendSmsCheckCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public @ResponseBody Object chengePwdSendSmsCheckCode(String phone, String ip) {
	    	memberService.chengePwdSendSmsCheckCode(phone, ip);
	        return ajaxDoneSuccess("验证码发送成功能");
	    }
	  
	    @RequestMapping(value = "/chengePwdDoSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public @ResponseBody Object chengePwdDoSet(String phone, String checkCode, String ip,String password,String password2) {
	    	memberService.chengePwdDoSet(phone, checkCode, ip, password, password2);
	        return ajaxDoneSuccess("密码重置成功");
	    }
	    
	    //====================================================================
	    
	    @RequestMapping(value = "/isTenantOpened", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public @ResponseBody Object isTenantOpened() {
	    	Optional<TenantMember> opMemberOp =	memberService.loadTenantMemberByUserId(SecurityUtil.getUserId().get());
	    	
	    	if(opMemberOp.isPresent()) {
	    		if(Byte.valueOf("2").equals(opMemberOp.get().getStatus())) {
	    			return ajaxDoneSuccess("开通成功").put("tenantId",  opMemberOp.get().getId());
	    		}else {
	    			return ajaxServerError("该账号所在多租户商状态不正常，请联系管理人员");
	    		}
	    	}
	        return ajaxDoneSuccess("没有开通过，进入开通协议页面").put("tenantId", null);
	    }
	    
	    
	    @RequestMapping(value = "/openTenant", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public @ResponseBody Object openTenant() {
	    	memberService.openTenant(SecurityUtil.getUserId().get());
	        return ajaxDoneSuccess("开通成功");
	    }
	    
}
